package com.netted.operationassistant.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;

import com.mylhyl.superdialog.SuperDialog;
import com.netted.operationassistant.constant.Constant;
import com.netted.operationassistant.view.IconHintView;
import com.blankj.utilcode.util.BarUtils;
import com.netted.operationassistant.R;
import com.netted.operationassistant.adapter.TitleAdapter;
import com.netted.operationassistant.view.MyRollPagerView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MenuActivity extends BaseActivity {

    Context context;
    boolean timeThread = true;

    private int hideCount = 0;
    //底部申明文本
    private TextView tv;

    private TextView dateTv;
    private TextView timeTv;
    private TextView weekTv;
    private Handler handler;

    private MyRollPagerView mRollViewPager;

    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        context = this;
        mRollViewPager = (MyRollPagerView) findViewById(R.id.titleLayout);
        //设置播放时间间隔
        mRollViewPager.setPlayDelay(3000);
        //设置透明度
        mRollViewPager.setAnimationDurtion(100);
        //设置适配器
        mRollViewPager.setAdapter(new TitleAdapter(this));
        mRollViewPager.setAnimation(new AlphaAnimation(3,2));
        mRollViewPager.setHintView(new IconHintView(this, R.drawable.point_focus, R.drawable.point_normal));
//        mRollViewPager.setHintView(new ColorPointHintView(this, Color.YELLOW,Color.WHITE));



        tv = (TextView)findViewById(R.id.main_sm_tv);
        dateTv = (TextView)findViewById(R.id.date_tv);
        timeTv = (TextView)findViewById(R.id.time_tv);
        weekTv = (TextView)findViewById(R.id.week_tv);
//        ToastUtils.setBgColor(ContextCompat.getColor(this, R.color.colorAccent));
        BarUtils.setStatusBarVisibility(this,false);
        String html = "本服务由<font color='#1C82FE'>珠海慧东方科技有限公司</font>提供";

        handler = new Handler();
        tv.setText(Html.fromHtml(html, null, null));

        refreshTime();
    }


    public void toGetDevicePage(View view){
//        Intent intent = new Intent(this,GetDevice2Activity.class);
//        startActivity(intent);
    }



    public void refreshTime(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(timeThread){
                    try {
                        final String time = new SimpleDateFormat("yyyy/MM/dd HH:mm").format(new Date());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                dateTv.setText(time.split(" ")[0]);
                                timeTv.setText(time.split(" ")[1]);
                                weekTv.setText(getWeekOfDate(new Date()));
                            }
                        });
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        timeThread = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }




    //取设备
    public void getDivece(View view){
        boolean b = checkPortIsClose();

        if(!b){
            showDialog("提示", Constant.checkAllPortIsCloseTips, null);
            return;
        }
        Intent intent = new Intent(this,GetDevice2Activity.class);
        startActivity(intent);
        this.finish();
    }

    //充电
    public void charge(View view){
        checkPortIsClose();
        if(!Constant.checkAllPortIsClose){
            showDialog("提示", Constant.checkAllPortIsCloseTips, null);
            return;
        }
        Intent intent = new Intent(this,Charge2Activity.class);
        startActivity(intent);
        this.finish();



        /*Intent itTrack = new Intent(this, FaceDetectExpActivity.class);
        startActivityForResult(itTrack, 1);*/
    }


    //归还平板
    public void returnDevice(View view){
        checkPortIsClose();
        if(!Constant.checkAllPortIsClose){
            showDialog("提示", Constant.checkAllPortIsCloseTips, null);
            return;
        }
        Intent intent = new Intent(this,ReturnDeviceActivity.class);
        intent.putExtra("from","MenuActivity");
        startActivity(intent);
        this.finish();
    }

    public void hideBtn(View view){
        hideCount ++;
        if (hideCount >= 5){
            hideCount = 0;
            Intent intent = new Intent(this,LoginActivity.class);
            startActivity(intent);
            this.finish();

        }
    }

//    public void getImage(View view){
//        HttpImageRequest request = new HttpImageRequest();
//        request.ImageRequest("https://pic.qqtn.com/up/2018-6/15293746617306806.jpg",new HttpImageRequest.Callback(){
//            @Override
//            public void onSuccess(Bitmap response) {
//
//            }
//
//            @Override
//            public void onError(VolleyError error) {
//                Toast.makeText(MenuActivity.this, "error:use default image", Toast.LENGTH_SHORT).show();
//            }
//        });
//    }
//
//
//    public void get(View view){
//        HttpRequest request = new HttpRequest();
//        request.get("http://baidu.com", "get", new HttpRequest.Callback() {
//            @Override
//            public void onSuccess(String response) {
//
//            }
//
//            @Override
//            public void onError(VolleyError error) {
//
//            }
//        });
//    }
//
//    public void post(View view){
//        Map<String,String> params = new HashMap<>();
//        params.put("wxId","oNChN5Vext98raz96nmaJPDi2Jd8");
//        params.put("account","13975910408");
//        HttpRequest request = new HttpRequest();
//        request.post("http://192.168.0.112/ws/user/get",params, "post", new HttpRequest.Callback() {
//            @Override
//            public void onSuccess(String response) {
//
//            }
//
//            @Override
//            public void onError(VolleyError error) {
//
//            }
//        });
//    }

}
